﻿using System;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DataAssociations.ConnectionOptionPartials;
using DataAssociations.Repository;
using System.Linq;
using DataAssociations.Repository.Connection.RDBMSConnectionInterfaces;

namespace DataAssociations
{
    public partial class ConnectForm : Form
    {
        private BaseUserControl _selectedOptionsInterface;

        public BaseUserControl SelectedOptionsInterface
        {
            get { return _selectedOptionsInterface; }
            set
            {
                buttonConnect.Enabled = true;
                _selectedOptionsInterface = value;
            }
        }

        public ConnectForm()
        {
            InitializeComponent();
            var supportedDBclasses = Reflectioner.FindImplementationsOfType<IConnectionStringType>(
                Assembly.Load("DataAssociations.Repository"));
            var supportedDbInstances = supportedDBclasses.Select(InstantiateDbInstances).ToList();
            listBoxSupportedRDBMS.Items.AddRange(
                supportedDbInstances.Select(x => x.DataBaseManagementSystemName).ToArray());
        }

        private IConnectionStringType InstantiateDbInstances(Type supportedDbType)
        {
            return (IConnectionStringType) Activator.CreateInstance(supportedDbType);
        }

        private void listBoxSupportedRDBMS_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxSupportedRDBMS.SelectedIndex >= 0)
            {
                string selectedItem = listBoxSupportedRDBMS.Items[listBoxSupportedRDBMS.SelectedIndex].ToString();
                var selectedRDBMS = selectedItem.Substring(0, selectedItem.IndexOf(" "));
                var type =
                    Assembly.GetExecutingAssembly().GetType("DataAssociations.ConnectionOptionPartials." + selectedRDBMS +
                                                            "ConnectionForm");
                SelectedOptionsInterface = (BaseUserControl) Activator.CreateInstance(type);
                panelConnectionOptions.Controls.Clear();
                panelConnectionOptions.Controls.Add(SelectedOptionsInterface);
            }
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (!SelectedOptionsInterface.FillAndValidateForm())
            {
                var validationErrors = new StringBuilder();
                foreach (var error in SelectedOptionsInterface.ValidationErrorsDictionary)
                    validationErrors.AppendLine(string.Format("The field {0} {1}", error.Key, error.Value));
                MessageBox.Show(validationErrors.ToString());
            }
            else
            {
                var analysisForm = new AnalysisForm(SelectedOptionsInterface.GetConnection());
                analysisForm.Show();
                Hide();
            }
        }
    }
}
